package cn.coisini.controller.v1;

import cn.coisini.api.admin.ArticleControllerApi;
import cn.coisini.model.common.annotation.RedisAccessLimit;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.common.log.annotation.Log;
import cn.coisini.model.common.log.enums.BusinessType;
import cn.coisini.model.dto.ArticleDto;
import cn.coisini.model.pojo.Article;
import cn.coisini.model.vo.QueryVo;
import cn.coisini.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiangshaw
 * Description: 文章控制层
 */
@RestController
@RequiredArgsConstructor
public class ArticleController implements ArticleControllerApi {

    private final ArticleService articleService;

    // 新增文章
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    public Result<ResultEnum> addArticle(ArticleDto articleDto) {
        return articleService.addArticle(articleDto);
    }

    // 条件分页 文章列表查询
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Object> getArticleList(QueryVo queryVo) {
        return articleService.getArticleList(queryVo);
    }

    // 根据文章id查询文章
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Article> getArticleById(String articleId) {
        return articleService.getArticleById(articleId);
    }

    // 根据文章id删除文章
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    public Result<ResultEnum> deleteArticle(String articleId) {
        return articleService.deleteArticle(articleId);
    }

    // 更新文章
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateArticle(ArticleDto articleDto) {
        return articleService.updateArticle(articleDto);
    }

    // 根据类别id统计文章数
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Long getArticleCountByCategoryId(String categoryId) {
        return articleService.getArticleCountByCategoryId(categoryId);
    }

    // 根据分类id查询文章列表
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public List<Article> getArticleListByIds(List<String> categoryIds) {
        return articleService.getArticleListByIds(categoryIds);
    }
    // 根据标签id查询文章列表
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public List<Article> getArticleListFromTag(String tagId) {
        return articleService.getArticleListFromTag(tagId);
    }
}
